<?php
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Uzytkownik_Model
 *
 * @author Właściciel
 */
class Logintrade_Model extends Model{
    public function pobierz_nadbranze()
    {
        $this->db = new Database('login2');
        $sql="select  distinct nadbranza.id_nadbranza,
     nadbranza.nazwa,
     nadbranza.nazwa_en,
     nadbranza.nazwa
    from  nadbranza,
     branza,
     branza_firma
    where branza_firma.id_firma=411 and
     branza_firma.id_branza=branza.id_branza and
     branza.id_nadbranza=nadbranza.id_nadbranza
    order by nadbranza.nazwa";
     return $this->db->query($sql)->result_array(false);
    }
    public function pobierz_branze($parametr=null)
    {
        $this->db = new Database('login2');
        if ($parametr!=null)
            {
            $sql="select
     distinct branza.id_branza,     branza.id_nadbranza,
     branza.nazwa,
          branza.nazwa_en,
     branza.nazwa_de
    from  branza,
     branza_firma
    where branza_firma.id_firma=411 and
     branza_firma.id_branza=branza.id_branza and branza.id_nadbranza=$parametr
    order by branza.nazwa limit 20";
            }
            else
                {
     $sql="select
     distinct branza.id_branza,     branza.id_nadbranza,
     branza.nazwa,
          branza.nazwa_en,
     branza.nazwa_de
    from  branza,
     branza_firma
    where branza_firma.id_firma=411 and
     branza_firma.id_branza=branza.id_branza
     order by branza.nazwa ";
    //order by branza.nazwa limit 20";
                }
        return $this->db->query($sql)->result_array(false);
    }
    public function pobierz_firmy($id_branza=null,$slowa=null,$_limit=null)
    {
        $this->db = new Database('login2');
        $sql="select distinct m.id_moi_dostawcy,m.nazwa_firmy
                    from moi_dostawcy m
                    inner join users u using(id_users)
                    inner join firma f using(id_firma)
                    inner join branza_firma bf using(id_firma)
                    inner join branza b using(id_branza)";
        if(!$id_branza)
        {
            //brak kategorii
            if ($slowa!=null)
            {
                $slowa=" where  (lower(m.nazwa_firmy) like lower(?) and lower(m.adres) like lower(?)) ".$_limit;
                return $this->db->query($sql,array($slowa,$slowa,$slowa))->result_array(false);
            }
            else
            {
                $sql.=$_limit;
                return $this->db->query($sql)->result_array(false);
            }
        }
        else
        {
            //podano kategorie
            $sql="select * from moi_dostawcy m inner join moi_dostawcy_branza mo using(id_moi_dostawcy) inner join branza br using(id_branza) ";
            $sql=$sql." where  br.id_branza=? ";
            if ($slowa!=null)
            {
                $slowa="%".$slowa."%";
                $sql.=" and  (lower(m.nazwa_firmy) like lower(?) or lower(m.adres) like lower(?)) ".$_limit;
                //var_dump($sql);
                $r=$this->db->query($sql,array($id_branza,$slowa,$slowa))->result_array(false);
                $r['licznik']=count($r);
                //var_dump($r);
                return $r;
            }
            else
            {
                $sql.=$_limit;
                return $this->db->query($sql,array($id_branza))->result_array(false);
            }
        }
        /*
        if($slowa)
            {
            $slowa=" and    lower(m.nazwa_firmy) like lower(?) and lower(m.adres) like lower(?) ";
            $sql.=" where ".substr($slowa,4).$_limit;
            }
        if($id_branza==null)
            {
            $sql="select distinct
                m.nazwa_firmy,m.adres,m.telefon,m.id_moi_dostawcy
                from
                moi_dostawcy m inner join moi_dostawcy_branza md using(id_moi_dostawcy)
                inner join branza b using(id_branza)
                inner join branza_firma bf using(id_branza)
                where bf.id_firma=411  ".$slowa.$_limit;
            if ($slowa)
                {
                $znak="%".$slowa."%";
                return $this->db->query($sql,array($id_branza,$znak,$znak))->result_array(false);
                }
            }
            else
            {
                //var_dump($this->db->query($sql,$id_branza)->result_array(false));
                return $this->db->query($sql.' and b.id_branza= ? ',$id_branza)->result_array(false);
            }
         *
         */
    }
}
?>
